package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.PersonPostAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.MyPublishBean;
import com.bnlr.lrparking.model.StallDetailBean;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 发布人信息
 * @author hp
 * @date 2017/10/17
 */

public class PersonageMaterialActivity extends BaseActivity {

    private RelativeLayout personage_post_list,personage_dele;
    private TextView personage_post_num,person_post_name,tv_personage,person_dele_name;
    private View personage_post_line,tv_personage_line;
    private ImageView personmater_back;
    private StallDetailBean.DataBean.UserModelData authBean;
    private Unbinder unbinder;
    @BindView(R.id.publish_name)
    TextView tvName;
    @BindView(R.id.imageView36)
    ImageView ivHead;
    @BindView(R.id.textView12)
    TextView registerDate;
    @BindView(R.id.imageView60)
    ImageView ivAuthStatus;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.recy_post)
    RecyclerView recyclerView;
    private PersonPostAdapter adapter;
    private List<MyPublishBean.DataBean> dataList = new ArrayList<>();
    private int currentPage = 1;
    private int status = 0;
    private int userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personagematerial);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        authBean = intent.getParcelableExtra("auth_bean");
        userId = intent.getIntExtra("user_id",0);
        initView();
        getMyPublishData(status,currentPage);
    }

    private void initView(){
        if (authBean != null) {
            tvName.setText(authBean.getName());
            Glide.with(this)
                    .load(authBean.getPhoto())
                    .bitmapTransform(new CropCircleTransformation(this))
                    .error(R.mipmap.personal_head)
                    .into(ivHead);
            String date = authBean.getCreateTime().split(" ")[0];
            registerDate.setText(date.split("-")[0] +"年"+date.split("-")[1] +"月"+date.split("-")[2] +"日注册");
            if ("1".equals(authBean.getIsRealNameAuth())) {
                ivAuthStatus.setImageResource(R.mipmap.auth_status_true);
            }
        }
        personage_post_list=(RelativeLayout)findViewById(R.id.personage_post_list);
        personage_dele=(RelativeLayout)findViewById(R.id.personage_dele);
        personage_post_num=(TextView) findViewById(R.id.personage_post_num);
        tv_personage=(TextView) findViewById(R.id.tv_bargain);
        person_post_name=(TextView) findViewById(R.id.person_post_name);
        person_dele_name=(TextView) findViewById(R.id.person_dele_name);
        personage_post_line=(View) findViewById(R.id.personage_post_line);
        tv_personage_line=(View) findViewById(R.id.tv_personage_line);
        personmater_back=(ImageView)findViewById(R.id.personmater_back);
        personage_post_list.setOnClickListener(this);
        personage_dele.setOnClickListener(this);
        personmater_back.setOnClickListener(this);
        rlPhone.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new PersonPostAdapter(dataList);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //  上拉加载
                currentPage++;
                getMyPublishData(status,currentPage);
            }
        },recyclerView);
       /* recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyPublishBean.DataBean md= dataList.get(position);
                if (20 == md.getTypeInt() || 21 == md.getTypeInt()) {
                    Intent intent = new Intent(PersonageMaterialActivity.this, StallBegRentBuy.class);
                    intent.putExtra("beg_detail", (Serializable) md);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(PersonageMaterialActivity.this, StallDealDetailActivity.class);
                    intent.putExtra("beg_detail", (Serializable) md);
                    startActivity(intent);
                }
            }
        });*/
        recyclerView.setAdapter(adapter);
        tv_personage.setText(authBean.getCompleteCount()+"");
        personage_post_num.setText(authBean.getCommonCount()+"");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.personage_post_list:
                setPostList();
                break;
            case R.id.personage_dele:
                setDeleList();
                break;
            case R.id.personmater_back:
                finish();
                break;
            case R.id.rl_phone:
                CarCommon carCommon = new CarCommon();
                carCommon.phoneDialog(this);
                break;
            default:
                break;
        }
    }
    //帖子
    private void setPostList(){
        personage_post_num.setTextColor(getResources().getColor(R.color.colorAccent));
        person_post_name.setTextColor(getResources().getColor(R.color.text_color));
        personage_post_line.setVisibility(View.VISIBLE);
        tv_personage.setTextColor(getResources().getColor(R.color.gray03_color));
        person_dele_name.setTextColor(getResources().getColor(R.color.gray03_color));
        tv_personage_line.setVisibility(View.GONE);
        status = 0;
        currentPage = 1;
        getMyPublishData(status,currentPage);
    }

    //资质
    private void setDeleList(){

        personage_post_num.setTextColor(getResources().getColor(R.color.gray03_color));
        person_post_name.setTextColor(getResources().getColor(R.color.gray03_color));
        personage_post_line.setVisibility(View.GONE);
        tv_personage.setTextColor(getResources().getColor(R.color.colorAccent));
        person_dele_name.setTextColor(getResources().getColor(R.color.text_color));
        tv_personage_line.setVisibility(View.VISIBLE);
        status = 3;
        currentPage = 1;
        getMyPublishData(status,currentPage);
    }
    private void getMyPublishData(int status,final int page) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "GetUserParkSpace");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("userId",userId);
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("pageIndex", page);
            mRequest.add("days", 30);
            mRequest.add("status",status);
            mRequest.add("isAll", 0);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<MyPublishBean>(this, true, MyPublishBean.class) {
                        @Override
                        public void doWork(MyPublishBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {

                                    if (data != null && data.getData().size() != 0) {
                                        if (page == 1) {
                                            dataList = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            dataList.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (page == 1) {
                                            dataList.clear();
                                            adapter.notifyDataSetChanged();
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }
                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                        }
                    }, false);
        } catch (Exception e) {
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
       unbinder.unbind();
    }
}
